package com.sagereal.wtc.factorymode.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.sagereal.wtc.factorymode.R;

public class BrightnessActivity extends BaseActivity {
    private Button mTestPass,mTestFail,mTestBtn;
    private boolean mIsRunning = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brightness);
        setTitle(getString(R.string.title_brightness_test));
        initView();
        startBrightnessCycle();
    }

    private void initView() {
        mTestPass = findViewById(R.id.test_pass);
        mTestFail = findViewById(R.id.test_fail);

        //测试通过
        mTestPass.setOnClickListener(v -> {
            Intent intent = new Intent(BrightnessActivity.this, ItemTestActivity.class);
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1)); // 获取启动该Activity时传递的ItemId
            intent.putExtra("status", "pass");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK , intent);
            finish();
        });
        //测试失败
        mTestFail.setOnClickListener(v -> {
            Intent intent = new Intent(BrightnessActivity.this, ItemTestActivity.class);
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1)); // 获取启动该Activity时传递的ItemId
            intent.putExtra("status", "fail");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK , intent);
            finish();
        });
    }

    private void startBrightnessCycle() {
        new Thread(() -> {
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            while (mIsRunning) {
                // 亮度变为最暗
                layoutParams.screenBrightness = 0.0f;
                runOnUiThread(() -> getWindow().setAttributes(layoutParams));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 亮度变为最亮
                layoutParams.screenBrightness = 1.0f;
                runOnUiThread(() -> getWindow().setAttributes(layoutParams));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void setNormalBrightness() {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE; // 恢复正常亮度
        getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            mIsRunning = true;
            startBrightnessCycle();
        } else {
            mIsRunning = false;
            setNormalBrightness(); // 恢复正常亮度
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mIsRunning = false;
        setNormalBrightness(); // 恢复正常亮度
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIsRunning = false;
        setNormalBrightness(); // 恢复正常亮度
    }
    @Override
    protected void onResume(){
        super.onResume();
        mIsRunning = true;
        startBrightnessCycle();
    }
}